﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yto.EF.Repositories;
using Yto.Entities;
using Yto.Repositories;

namespace Yto.EF.Extensions
{
    public static class EfRepositoryExtensions
    {
        public static DbContext GetDbContext<TE, TP>(this IBaseRepository<TE, TP> repository)
            where TE : class, IEntity<TP>
        {
            var efRepository = repository as IEfDbContext;
            if (efRepository == null)
            {
                throw new ArgumentException("The repository do not implement IEfDbContext!");
            }

            return efRepository.GetDbContext();
        }

        public static void DetachEntity<TE,TP>(this IBaseRepository<TE, TP> repository,TE entity)
             where TE : class, IEntity<TP>
        {
            repository.GetDbContext().Entry(entity).State = EntityState.Detached;
        }
    }
}
